package com.fengke.cn.yejian11.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.fengke.cn.yejian11.Model.Model;
import com.fengke.cn.yejian11.R;
import com.fengke.cn.yejian11.base.BaseActivity;
import com.fengke.cn.yejian11.impls.TrainImpls;
import com.fengke.cn.yejian11.javabean.TrainInfo;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Call<Integer> joinTrain(
 * @Query("participantname") String participantname,
 @Query("train_id") String train_id,
 @Query("participanttel") String participanttel,
 @Query("adultnum") String adultnum,
 @Query("paytype") String paytype,
 @Query("pay") String pay,
 @Query("paynotice") String paynotice,
 @Query("userid") String userid,
 @Query("SessionID") String SessionID);
 */
public class T_Join extends BaseActivity {
    private RadioButton aliapy,weixinpay;
    private TextView pPrice,notice,train_total_price;
    private TrainInfo info;
    private ImageView join_back;
    private Button join;
    private RadioGroup payway;
    private String participantname,train_id,participanttel,
                   adultnum,paytype,pay,paynotice,userid,SessionID;
    private EditText pName,pTel,pNum;
    private int number;
    private double totalmoney,singlemoney;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_t__join);
        init();
        setText();
        update();//更新界面价格
        getText();
        event();
    }

    private void update() {
        pNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!s.toString().equals("")){
                number=Integer.parseInt(s.toString());
                  totalmoney=number*(Integer.parseInt(info.getPrice()));
                train_total_price.setText(String.valueOf(totalmoney));
                Toast.makeText(T_Join.this,"s",Toast.LENGTH_SHORT).show();
                }


            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().equals(""))
                {
                    number=Integer.parseInt(s.toString());
                    totalmoney=number*(Integer.parseInt(info.getPrice()));
                    train_total_price.setText(String.valueOf(totalmoney));
                }
                else{
                    train_total_price.setText("0");
                }
            }
        });
    }

    private void getText() {
        participantname=pName.getText().toString();
        if(info!=null)
        {
            train_id=info.getId();
            pay=info.getPrice();
        }
        else{
            Toast.makeText(T_Join.this, "部分数据为空", Toast.LENGTH_SHORT).show();
        }
        participanttel=pTel.getText().toString();
        adultnum=pNum.getText().toString();
        userid=getUserId();
        SessionID="pf2cmg5jk9p24f2mv1kjf55bo3";
        paynotice=notice.getText().toString();
    }

    private void setText() {
        if (info!=null)
        {
            pPrice.setText("￥"+info.getPrice()+"/人");
        }
        else{
            pPrice.setText("");
        }
        totalmoney=Integer.parseInt(pNum.getText().toString())*Integer.parseInt(info.getPrice());
        train_total_price.setText(String.valueOf(totalmoney));
    }

    private void event() {
        payway.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId)
                {
                    case R.id.train_join_alipay:
                       paytype="0";
                        break;
                    case R.id.train_join_weixinpay:
                        paytype="1";
                        break;
                    default:
                        Toast.makeText(T_Join.this,"您还没有选择支付方式",Toast.LENGTH_SHORT).show();
                }
            }
        });
        join.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (paytype==null||paytype=="")
                {
                    Toast.makeText(T_Join.this,"您还没有选择支付方式",Toast.LENGTH_SHORT).show();
                    return;
                }
                Retrofit re= Model.getConn();
                TrainImpls service=re.create(TrainImpls.class);
                Call<Integer> call=service.joinTrain(
                        participantname,train_id,
                        participanttel,adultnum
                        ,paytype,pay,paynotice,
                        userid,SessionID);
                call.enqueue(new Callback<Integer>() {
                    @Override
                    public void onResponse(Call<Integer> call, Response<Integer> response) {
                        Integer i=response.body();
                        if (i==0||i==null)
                        {
                            return;
                        }
                        Toast.makeText(T_Join.this, "加入成功"+i, Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onFailure(Call<Integer> call, Throwable t) {
                        Toast.makeText(T_Join.this, "加入失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    private void init() {
        train_total_price=(TextView)findViewById(R.id.train_total_price);
        payway=(RadioGroup)findViewById(R.id.payway);
        join=(Button)findViewById(R.id.train_btn_jion);
        join_back=(ImageView)findViewById(R.id.train_join_back);
        pName=(EditText)findViewById(R.id.train_participainname);
        pTel=(EditText)findViewById(R.id.train_tel);
        pNum=(EditText)findViewById(R.id.train_num) ;
        pPrice=(TextView)findViewById(R.id.train_price) ;
        Intent i=getIntent();
        notice=(TextView)findViewById(R.id.notice);
        //得到TrainActivity传过来的参数
        info =(TrainInfo) i.getParcelableExtra("trainInfo");
        aliapy=(RadioButton)findViewById(R.id.train_join_alipay);
        weixinpay=(RadioButton)findViewById(R.id.train_join_weixinpay);
        number=0;
    }
}
